package com.ruoyi.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.api.annotation.Login;
import com.ruoyi.business.app.bo.AppDirectBo;
import com.ruoyi.business.app.bo.AppSecondBo;
import com.ruoyi.business.app.service.IMyTeamService;
import com.ruoyi.business.app.vo.*;
import com.ruoyi.business.domain.HcxAppUser;
import com.ruoyi.business.service.IHcxAppUserService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.core.vo.ObsUploadRespVo;
import com.ruoyi.common.utils.AssertUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.oss.OssUtils;
import com.ruoyi.common.utils.qrcode.QRCodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


@Api(tags = "我的--->我的团队")
@AllArgsConstructor
@RestController
@RequestMapping("/api/my/team")
public class MyTeamController extends BaseController {

    private final IMyTeamService myTeamService;

    private final IHcxAppUserService appUserService;

    private final RedisCache redisCache;

    private final OssUtils ossUtils;

    @ApiOperation("团队收益")
    @GetMapping("/profit")
    @Login
    public AjaxResult<AppMyProfitVo> profit() {
        AppMyProfitVo profit = myTeamService.profit();
        return AjaxResult.success(profit);
    }


    @ApiOperation("查询我的收益明细列表")
    @GetMapping("/profitList")
    @Login
    public TableDataInfo<AppWithdrawDetailVo> profitList(@ApiParam(value = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
                                                         @ApiParam(value = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize) {
        return myTeamService.queryZhangdanPageList(pageNum, pageSize);
    }


    /**
     * 查询一级成员列表
     */
    @ApiOperation("查询一级成员列表")
    @GetMapping("/directList")
    @Login
    public TableDataInfo<AppDirectVo> directList(AppDirectBo bo) {
        return myTeamService.queryAppPageList(bo);
    }

    @ApiOperation("查询一级成员列表中的某个成员信息")
    @GetMapping("/directDetail")
    @Login
    public AjaxResult<AppDirectDetailVo> directDetail(@ApiParam(value = "用户id") Long userId) {
        AppDirectDetailVo profit = myTeamService.directDetail(userId);
        return AjaxResult.success(profit);
    }

    /**
     * 查询二级成员列表
     */
    @ApiOperation("查询二级成员列表")
    @GetMapping("/secondList")
    @Login
    public TableDataInfo<AppSecondVo> secondList(AppSecondBo bo) {
        return myTeamService.queryAppSecondPageList(bo);
    }

    @ApiOperation("推广海报")
    @GetMapping("/myShareCode")
    @Login
    public AjaxResult<MyShareCodeVo> myShareCode() throws Exception {

        // 设置logo小图标
        String logoAddress = redisCache.getCacheObject(Constants.SYS_CONFIG_KEY + Constants.QRCODE_LOGO_ADDRESS);
        AssertUtils.isNotBlank(logoAddress, "logo地址还没有配置,请后台配置!");
        // 分享二维码存放目录
        String destPath = redisCache.getCacheObject(Constants.SYS_CONFIG_KEY + Constants.QRCODE_DESC_ADDRESS);
        AssertUtils.isNotBlank(destPath, "二维码存放地址还没有配置,请后台配置!");


        String sharePath = redisCache.getCacheObject(Constants.SYS_CONFIG_KEY + Constants.YAOQINGDIZHI);
        HcxAppUser hcxAppUser = appUserService.getOne(new LambdaQueryWrapper<HcxAppUser>().eq(HcxAppUser::getId, SecurityUtils.getUserId()));
        MyShareCodeVo vo = new MyShareCodeVo();

        // 生成二维码
        MultipartFile multipartFile = QRCodeUtil.encode(sharePath + "?inviteCode=" + hcxAppUser.getInviteCode(), logoAddress, destPath, true);
        // 文件上传
        ObsUploadRespVo obsUploadRespVo = ossUtils.uploadMultipartFile(multipartFile);
        vo.setInviteQrcode(obsUploadRespVo.getObjectUrl());
        vo.setInviteUrl(sharePath + "?inviteCode=" + hcxAppUser.getInviteCode());
        return AjaxResult.success(vo);
    }
}
